const app = getApp()
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
var xrSdk = require('../../../vendor/hope-sdk/index.js');
const util = require('../../../utils/util.js');
Page({
  data: {
    tabs: [{
      name: "全部"
    }, {
      name: "待审核"
    }, {
      name: "已通过"
    }, {
      name: "未通过"
    }, {
      name: "已替换"
    }],
    key: "",
    currentTab: 0,
    pageIndex: 1,
    loadding: false,
    pullUpOn: true,
    noContent: false,
    scrollTop: 0,
    arrowTop: 0, //箭头距离顶部距离
    tyreList: [],
    first: true
  },
  onLoad: function (options) {
    var that = this;
    let obj = wx.getMenuButtonBoundingClientRect();
    that.setData({
      width: obj.left,
      height: obj.top + obj.height + 8,
      inputTop: obj.top + (obj.height - 30) / 2,
      arrowTop: obj.top + (obj.height - 32) / 2,
      currentTab: options.currentTab || 0
    }, () => {
      wx.getSystemInfo({
        success: (res) => {
          that.setData({
            //略小，避免误差带来的影响
            dropScreenH: that.data.height * 750 / res.windowWidth - 65
          })
        }
      });
      that.initList();
    });
  },
  change(e) {
    this.setData({
      currentTab: e.detail.index
    })
    this.onPullDownRefresh();
  },
  detail: function (e) {
    var tyre_id = e.currentTarget.dataset.id;
    app.WxService.navigateTo("../info/info", {
      id: tyre_id
    });
  },
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  initData: function () {
    this.setData({
      tyreList: [],
      pageIndex: 1,
      pullUpOn: true,
      loadding: true,
      noContent: false
    })
  },
  initList: function () {
    var that = this;
    var params = that.getParams();
    xrRequest.request(app,{
      url: config.service.tyreList,
      login: true,
      data: params,
      success: function (res) {
        that.setData({
          tyreList: that.data.tyreList.concat(res.data.data.data),
          pageIndex: that.data.pageIndex + 1,
          loadding: false
        })
        // 没有更多
        if (res.data.data.has_more == false &&
          that.data.tyreList.length > 0) {
          that.setData({
            pullUpOn: false
          })
        }
        // 空数据
        that.setData({
          noContent: that.data.tyreList.length == 0
        });
        wx.stopPullDownRefresh();
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    });
  },
  search: function (e) {
    this.initData();
    this.initList();
  },
  input: function (e) {
    let key = util.trim(e.detail.value);
    this.setData({
      key: key
    })
  },
  cleanKey: function () {
    this.setData({
      key: ''
    });
  },
  getParams() {
    var that = this;
    var params = {
      page: that.data.pageIndex,
      page_size: 10,
      status: that.data.currentTab,
      keyword:that.data.key
    };
    return params;
  },
  onPullDownRefresh() {
    this.initData();
    this.initList();
  },
  onReachBottom() {
    if (!this.data.pullUpOn) return;
    this.setData({
      loadding: true
    }, () => {
      this.initList();
    })
  },
  onClick: function (e) {
    wx.redirectTo({
      url: '../../activity/apply/apply',
    })
  },
  onShow: function () {
    if (!this.data.first) {
      this.initData();
      this.initList();
    }
    this.setData({
      first: false
    });
  },
  back: function () {
    wx.navigateBack()
  }
})